package controllers

import (
	"log"
	"net/http"

	"gitee.com/yanxingshuyuan/backend/server/models"
	"gitee.com/yanxingshuyuan/backend/server/repositories"
	"github.com/gin-gonic/gin"
)

type StatsController struct {
	Repo *repositories.StatsRepo
}

func NewStatsController(r *repositories.StatsRepo) *StatsController {
	return &StatsController{
		Repo: r,
	}
}

func (ctrl *StatsController) ListStatsForMats(c *gin.Context) {
	user, _ := c.Get("jwtuser")
	mypid := user.(*models.User).Pid
	var mids []string
	err := c.BindJSON(&mids)
	if err != nil {
		log.Printf("Error getting post param mids: %v\n", err)
	}
	stats := ctrl.Repo.ListStatsForMats(mids, mypid)
	c.JSON(http.StatusOK, stats)
}

func (ctrl *StatsController) IncStar(c *gin.Context) {
	mid := c.Param("mid")
	user, _ := c.Get("jwtuser")
	pid := user.(*models.User).Pid
	stars := ctrl.Repo.IncStar(mid, pid)
	c.JSON(http.StatusOK, stars)
}
